package com.example.mobile203zlw20216061300611;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    SQLiteOpenHelper sqLiteOpenHelper;
    SQLiteDatabase database;
    Context context;
    private TextView tvMainTitle;
    private EditText edtTxtMainName;
    private EditText edtTxtMainAge;
    private EditText edtTxtMainScore;
    private Button btnMainInsert;
    private Button btnMainUpdate;
    private Button btnMainDelete;
    private Button btnMainQuery;
    private TextView tvMainDisplay;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        sqLiteOpenHelper = new Sqlist(this, "student.db", null, 1);
        initView();
    }

    private void initView() {
        tvMainTitle = (TextView) findViewById(R.id.tvMainTitle);
        edtTxtMainName = (EditText) findViewById(R.id.edtTxtMainName);
        edtTxtMainAge = (EditText) findViewById(R.id.edtTxtMainAge);
        edtTxtMainScore = (EditText) findViewById(R.id.edtTxtMainScore);
        btnMainInsert = (Button) findViewById(R.id.btnMainInsert);
        btnMainUpdate = (Button) findViewById(R.id.btnMainUpdate);
        btnMainDelete = (Button) findViewById(R.id.btnMainDelete);
        btnMainQuery = (Button) findViewById(R.id.btnMainQuery);
        tvMainDisplay = (TextView) findViewById(R.id.tvMainDisplay);
    }

    public void SQL_add(View view) {
        String name=edtTxtMainName.getText().toString();
        int age= Integer.parseInt(edtTxtMainAge.getText().toString());
        float score= Float.parseFloat(edtTxtMainScore.getText().toString());
        ContentValues values=new ContentValues();
        values.put("name",name);
        values.put("age",age);
        values.put("score",score);
        database=sqLiteOpenHelper.getWritableDatabase();
        long Long =database.insert("student",null,values);
        database.close();
    }

    public void SQL_xiu(View view) {
        String name=edtTxtMainName.getText().toString();
        int age= Integer.parseInt(edtTxtMainAge.getText().toString());
        float score= Float.parseFloat(edtTxtMainScore.getText().toString());
        ContentValues values=new ContentValues();
        values.put("name",name);
        values.put("age",age);
        values.put("score",score);
        database=sqLiteOpenHelper.getWritableDatabase();
        database.update("student",values,"name=?",new  String []{name});
        database.close();
    }

    public void SQL_colse(View view) {
        String name=edtTxtMainName.getText().toString();
        database=sqLiteOpenHelper.getWritableDatabase();
        database.delete("student", "name=?", new String[]{name});
        database.close();
    }

    public void SQL_cha(View view) {
        database=sqLiteOpenHelper.getReadableDatabase();
        Cursor cursor=database.query("student",null,null,null,null,null,null);
        if (cursor.moveToFirst()){
            tvMainDisplay.setText(null);
            do{
                String name=cursor.getString(1);
                int age= cursor.getInt(2);
                float score= cursor.getFloat(3);
                tvMainDisplay.append(name+"\n"+age+"\n"+score);
            }while (cursor.moveToNext());
        }
    }
}